// 橙色页
//  MZY_OrangeViewController.m
//  FullHouse
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_OrangeViewController.h"
#import "NetworkingTool.h"
#import "UIImageView+WebCache.h"
#import "MZY_HomePageModel.h"
#import "MZY_IntroduceCell.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MZY_OrangeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *arr;

@end

@implementation MZY_OrangeViewController

- (void)dealloc
{
    [self.tableView release];
    [self.arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createData];
    [self createView];
}


#pragma mark 获取数据
- (void)createData{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.meilijia.com/app/featured_subject?subject_id=%@&page=1",self.idStr];
    [NetworkingTool networkingWithStrURL:urlStr type:GETType bodyStr:nil block:^(id result) {
        self.arr = [NSMutableArray array];
        for (NSDictionary *temp in result[@"data"][@"featured_subject"][@"info"][@"collections"]) {
            MZY_HomePageModel *home = [[MZY_HomePageModel alloc]init];
            [home setValuesForKeysWithDictionary:temp];
            [self.arr addObject:home];
            [home release];
            [self.tableView reloadData];
        }
    }];
}

#pragma mark 创建界面
- (void)createView{
    
    self.view.backgroundColor = [UIColor cyanColor];
    
    self.title = self.titleStr;
    
    // 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, HEIGHT - 64 - 20) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 270;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[MZY_IntroduceCell class] forCellReuseIdentifier:@"table"];
    [self.tableView release];
}

#pragma mark "tableView"的第一个必须执行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}

#pragma mark "tableView"的第二个必须执行的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MZY_IntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
    MZY_HomePageModel *home = self.arr[indexPath.row];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:home.cover_pic]];
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:home.author_face]];
    cell.headImageView.layer.cornerRadius = 30;
    cell.headImageView.layer.masksToBounds = YES;
    cell.titleLabel.text = home.title;
    cell.titleLabel.textColor = [UIColor orangeColor];
    cell.introduceLabel.text = home.title_sub;
    cell.introduceLabel.font = [UIFont systemFontOfSize:15];
    [cell.likeButton setImage:[UIImage imageNamed:@"zan.png"] forState:UIControlStateNormal];
    [cell.likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.likeCountLabel.text = home.liked_count;
    cell.likeCountLabel.textAlignment = NSTextAlignmentCenter;
    NSString *priceStr = home.work_design_price;
    cell.priceLabel.text = [NSString stringWithFormat:@"%@%@",priceStr,@"元/平米"];
    cell.priceLabel.textAlignment = NSTextAlignmentCenter;
    cell.priceLabel.alpha = 0.6;
    return cell;
}

#pragma mark 赞"button"的点击方法
- (void)likeAction:(UIButton *)button{
    
}

#pragma mark 返回按钮的点击方法
- (void)backAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
